<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" keyword="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <el-table @row-dblclick="getClick" :data="dictionaryType" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">

      <!--ui提供的多选框-->
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="120" >
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="120" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
      <!--:disabled是否不可点击
              true:不可点击
              false: 可点击
        -->
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="total"  style="float:right;">
			</el-pagination>
		</el-col>

    <!--prop 校验规则-->
		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据字典名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>


			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
    <!--抽屉-->
    <el-drawer
        title="字典数据明细!"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="dictionaryData" highlight-current-row v-loading="listLoading" @selection-change="selsChangeDrawer" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="数据明细" width="120" >
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="handleAddDrawer">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="handleEditDrawer(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelDrawer(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemoveDrawer" :disabled="this.selsDrawer.length===0">批量删除</el-button>
        <el-pagination
            @current-change="handleCurrentChangeDrawer"
            :page-size="pageSizeDrawer"
            :total="totalDrawer"
            style="float:right;"
            :current-page="currentPageDrawer"
            :page-sizes="[5, 10, 15, 20]"
            layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
      <!--抽屉编辑模块-->
    </el-drawer>

    <el-dialog :title="titleDrawer" :visible.sync="editFormVisibleDrawer" :close-on-click-modal="true">
      <el-form :model="editFormDrawer" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="数据明细" prop="name">
          <el-input v-model="editFormDrawer.name" auto-complete="off"></el-input>
        </el-form-item>
      <!--  <el-form-item label="所属分类" prop="types_id">
          <el-input v-model="editFormDrawer.types_id" auto-complete="off"></el-input>
        </el-form-item>
-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisibleDrawer = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmitDrawer" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>


	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
        table:false,
        dictionaryData:[],
        currentPageDrawer:1,
        pageSizeDrawer:5,
        totalDrawer:0,
        type_id:null,
        datatypesId:null,
        editFormVisibleDrawer:false,
        // 抽屉编辑页面
        editFormDrawer: {
          id: null,
          name:null,
          types_id:null,
        },
        titleDrawer:"",
        selsDrawer:[],




        options: [],
				filters: {
					keyword: ''
				},
        cities: [],
        dictionaryType:[],

				total: 0,
				currentPage: 1,
        pageSize:5,
				listLoading: false,
				sels: [],//列表选中列
        title:"",
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
              // 为空校验
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					],
          sn: [
              // 为空校验
						{ required: true, message: '请输入部门编号', trigger: 'blur' }
					]

				},
				//编辑界面数据
				editForm: {
					id: null,
					name: "",
          types_id:this.datatypesId
				},
			}
		},
		methods: {
      // 抽屉分页
      handleCurrentChangeDrawer(val){
          this.currentPageDrawer = val,
          this.getDictionaryData(this.datatypesId)

      },
      getClick(row){
        this.getDictionaryData(row.id)
        this.table =true

      },
      getDictionaryData(id) {
        // 封装参数
        let para = {
          currentPage: this.currentPageDrawer,
          pageSize:this.pageSizeDrawer,
          types_id:id
        };
        this.$http.post("/dictionary",para)
            .then(result=>{
              this.datatypesId = id;
              this.dictionaryData = result.data.rows;
              this.totalDrawer = result.data.totals;
              console.log(this.totalDrawer)
            })
        },

      // 抽屉删除
      handleDelDrawer:function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.$http.delete("/dictionary/"+row.id)
              .then(result=>{
                if (result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;
                // 4.调用查询列表函数
                this.getDictionaryData(this.datatypesId);
              }).catch(result=>{
            this.listLoading =false;
            this.$message({
              message: '系统繁忙请稍后重试！！！',
              type: 'error'
            });

          })
        }).catch(() => {
          // 取消进catch
        });

      },

      // 抽屉显示编辑界面
      handleEditDrawer(index, row) {
        this.titleDrawer = "编辑";
        // 弹出编辑框
        this.editFormVisibleDrawer = true;
        // 克隆对象到我们的模型层的对象中
        this.editFormDrawer = Object.assign({}, row);
        this.editFormDrawer = this.datatypesId;
      },
      //显示新增界面
      handleAddDrawer: function () {
        this.titleDrawer = "新增";
        this.editFormVisibleDrawer = true;
        this.editFormDrawer = {
          id: null,
          name: "",
          types_id: this.datatypesId
        };
      },

      editSubmitDrawer: function () {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              // 表单数据拷贝
              let para = Object.assign({}, this.editFormDrawer);

              // 发送axios请求
              this.$http.put("/dictionary",para)
                  .then(result=>{
                    // 关闭模态框
                    this.editFormVisibleDrawer = false;
                    if (result.data.success){
                      this.$message({
                        message: result.data.message,
                        type: 'success'
                      });

                    }else {
                      this.$message({
                        message: result.data.message,
                        type: 'error'
                      });
                    }
                    this.currentPage = 1
                    this.getDictionaryData(this.datatypesId)
                  }).catch(result=>{
                this.editLoading = false;
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })
            }).catch(()=>{

            });
          }
        });
      },
      selsChangeDrawer: function (selsDrawer) {
        this.selsDrawer = selsDrawer;

      },
      // 抽屉的批量删除
      //批量删除
      batchRemoveDrawer() {
        // 获取选中行的id
        var idsDrawer = this.selsDrawer.map(item => item.id);

        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          // 发送 axios请求
          this.$http.patch("/dictionary", idsDrawer)
              .then(result => {
                if (result.data.success) {
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                // 删除后回到第一页
                this.currentPage = 1
                // 刷新列表
                this.getDictionaryData(this.datatypesId)
              }).catch(result => {
            this.listLoading = false;
            this.$message({
              message: '系统繁忙请稍后重试！！！',
              type: 'error'
            });

          }).catch(() => {

          });
        })
      },

      getTree(){
        this.$http.get("/department/tree")
            .then(result=>{
              this.options = result.data
            })
      },
      getEmployee(){
        this.$http.get("/employee")
            .then(result=>{
              this.cities=result.data
            })
      },

			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionaryType();
			},
      // 高级查询
      search(){
        this.currentPage=1
        this.getDictionaryType()

      },
			//获取部门列表
			getDictionaryType() {
        // 封装参数
				let para = {
          currentPage: this.currentPage,
          pageSize:this.pageSize,
					keyword: this.filters.keyword,
        };
				this.listLoading = true;
        this.$http.post("/dictionaryType",para)
                .then(result=>{
                  this.listLoading = false;
                    this.dictionaryType = result.data.rows;
                    this.total = result.data.totals;

                }).catch(result=> {
          this.$message({
            message: '系统繁忙请稍后重试！！！',
            type: 'error'
          })

        })

			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.delete("/dictionaryType/"+row.id)
              .then(result=>{
                // 关闭忙等
                this.listLoading =false;
                if (result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;
                // 4.调用查询列表函数
                this.getDictionaryType();
              }).catch(result=>{
            this.listLoading =false;
            this.$message({
              message: '系统繁忙请稍后重试！！！',
              type: 'error'
            });

          })
        }).catch(() => {
          // 取消进catch
        });

			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title = "编辑";
        // 弹出编辑框
				this.editFormVisible = true;
        // 克隆对象到我们的模型层的对象中
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.title = "新增";
				this.editFormVisible = true;
				this.editForm = {
          id: null,
          name: "",
          sn: "",
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
              // 开启忙等
							this.editLoading = true;

              // 表单数据拷贝
							let para = Object.assign({}, this.editForm);
              if (para.parent){
                para.parent = {id:para.parent[para.parent.length - 1]}
              }
              // 发送axios请求
                this.$http.put("/dictionaryType",para)
                    .then(result=>{
                      // 关闭忙等
                      this.editLoading = false;
                      // 关闭模态框
                      this.editFormVisible = false;
                        if (result.data.success){
                          this.$message({
                            message: result.data.message,
                            type: 'success'
                          });

                        }else {
                          this.$message({
                            message: result.data.message,
                            type: 'error'
                          });
                        }
                        this.currentPage = 1
                        this.getDictionaryType()
                    }).catch(result=>{
                  this.editLoading = false;
                  this.$message({
                    message: '系统繁忙，请重试！',
                    type: 'error'
                  });
                })
						}).catch(()=>{

            });
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;

			},
			//批量删除
			batchRemove: function () {
        // 获取选中行的id
        var ids = this.sels.map(item => item.id);

        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          // 开启列表忙等
          this.listLoading = true;
          // 发送 axios请求
          this.$http.patch("/dictionaryType", ids)
              .then(result => {
                // 关闭忙等
                this.listLoading = false;
                if (result.data.success) {
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                } else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                // 删除后回到第一页
                this.currentPage = 1
                // 刷新列表
                this.getDictionaryType()
              }).catch(result => {
              this.listLoading = false;
              this.$message({
              message: '系统繁忙请稍后重试！！！',
              type: 'error'
            });

          }).catch(() => {

          });
        })
      }
		},
		mounted() {
			this.getDictionaryType()
      this.getEmployee();
      this.getTree();
		}
	}

</script>

<style scoped>

</style>